package com.qf.dao.Impl;/*
    Dell
    2021/10/2
    12:42
*/

import com.qf.dao.OrganDao;
import com.qf.entiy.Health;
import com.qf.entiy.Organ;
import com.qf.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class OrganDaoImpl implements OrganDao {
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<Organ> showAllOrgan() {
        try {
            return queryRunner.query(DruidUtil.getConnection(),"select * from Organ;", new BeanListHandler<>(Organ.class));
        } catch (SQLException e) {
           throw new RuntimeException(e.getMessage()+":查询所有机构出现了异常");
        }
    }

    @Override
    public Health select(Integer id) {
        try {
            String sql = "select id, projectname, status, money, deadline, h.organId, organname from health as h inner join organ o on h.organId = o.organId where id = ?;";
            return queryRunner.query(DruidUtil.getConnection(),sql,new BeanHandler<>(Health.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":查询单个对象出现了异常");
        }
    }
}
